package com.breaktian.assemble.socket.tcp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;

import android.util.Log;

import com.breaktian.assemble.socket.callback.SendTcpUTFCallBack;


/**
 * @author breaktian
 * tcp协议
 * 客户端
 * */
public class TcpSocket {

	private Socket tcpSocket;
	
	private int timeout = 100000;//100秒

	public TcpSocket(){
		tcpSocket = new Socket();
	}
	
	
	
	public TcpSocket connect(String remoteIp, int remotePort){
		if(tcpSocket==null) throw new IllegalStateException("tcpSocket为null");
		try {
			SocketAddress remoteAddr = new InetSocketAddress(InetAddress.getByName(remoteIp), remotePort);
			tcpSocket.connect(remoteAddr,timeout);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this;
	}
	
	
	/**
	 * 发送数据 64kb以下
	 * 有返回数据
	 * */
	public void sendUTF(String s){
		if(tcpSocket==null) throw new IllegalStateException("tcpSocket为null");
		try {
			DataOutputStream dos = new DataOutputStream(tcpSocket.getOutputStream());
			
			dos.writeInt(TcpServerSocket.SEND_TYPE_UTF_NO_BACK);
			dos.writeUTF(s);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送数据 64kb以下
	 * 有返回数据
	 * */
	public void sendUTF(String s, SendTcpUTFCallBack call){
		if(tcpSocket==null) throw new IllegalStateException("tcpSocket为null");
		try {
			DataOutputStream dos = new DataOutputStream(tcpSocket.getOutputStream());
			DataInputStream dis = new DataInputStream(tcpSocket.getInputStream());
			
			dos.writeInt(TcpServerSocket.SEND_TYPE_UTF);
			dos.writeUTF(s);
			String recv = dis.readUTF();
			if(call!=null) call.onReceive(recv);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送byte[]数据
	 * 不需要返回数据的使用此方法
	 * */
	public void sendData(byte[] data){
		if(tcpSocket==null) throw new IllegalStateException("tcpSocket为null");
		try {
			DataOutputStream dos = new DataOutputStream(tcpSocket.getOutputStream());
			dos.writeInt(TcpServerSocket.SEND_TYPE_DATA);
			dos.writeLong(data.length);
			dos.write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	
	/**
	 * 关闭socket
	 * */
	public void close(){
		if(tcpSocket==null) throw new IllegalStateException("tcpSocket为null");
		try {
			tcpSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 读取数据
	 */
	public void ReadData(){		
		try {
			InputStream mmInStream = tcpSocket.getInputStream();
			if(mmInStream == null){
				return;
			}
			ArrayList<Integer> arr_byte = new ArrayList<Integer>();
			byte[] buffer;
			while (true) {	          
	                int data = mmInStream.read();
	                if(data == 0x0A) { 
	                } else if(data == 0x0D) {
	                    buffer = new byte[arr_byte.size()];
	                    for(int i = 0 ; i < arr_byte.size() ; i++) {
	                        buffer[i] = arr_byte.get(i).byteValue();
	                    }
	                    Log.d("TcpSocket","client <<<<<<<<< " + buffer.toString());
	
	                    arr_byte = new ArrayList<Integer>();
	                } else {
	                }	          
	        }

		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	
	
	
}
